﻿Imports System.Windows.Forms
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Runtime.Serialization
Imports System.Xml.Serialization
Public Class RIL

    <Serializable()>
    <XmlRoot("RILCredentials")>
    Public Class Credentials
        Inherits RIL
        Public Shared Event UserLogin(ByVal UserName As String)
        Public Shared Event UserLogout(ByVal UserName As String)
        Public Shared Event UserLoginError(ByVal UserName As String, ByVal [Error] As Exception)
        Public Shared Event [Error](ByVal [Error] As Exception)

        Private Shared P_PublicKey As String
        Private P_Credentials As New List(Of User)

        <Serializable()>
        Public Class User
            Inherits Credentials
            Implements IXmlSerializable


            Private P_UserName As String = ""
            Private P_UserPassword As String = ""
            Private P_UserLogin As Boolean = False
            Private P_LOGINConString As String = ""

            Public Sub New(ByVal UserName As String, ByVal Password As String)
                Try
                    Me.UserName = UserName
                    Me.UserPassword = Password
                Catch ex As Exception
                    'RaiseEvent Error(ex)
                End Try
            End Sub

            Public Sub New()

            End Sub

            Public Property UserName As String
                Get
                    Return P_UserName
                End Get
                Set(ByVal value As String)
                    P_UserName = value
                    P_UserLogin = False
                End Set
            End Property

            Public Property UserPassword As String
                Get
                    Return P_UserPassword
                End Get
                Set(ByVal value As String)
                    P_UserPassword = value
                    P_UserLogin = False
                End Set
            End Property

            Public ReadOnly Property UserLogedIn As Boolean
                Get
                    Return P_UserLogin
                End Get
            End Property

            Public Function Login() As Boolean
                Try

                    P_LOGINConString = "username=" & P_UserName & "&password=" & P_UserPassword & "&apikey=" & PublicKey

                    Dim LoginThread As New Threading.Thread(AddressOf LoginWorker)
                    LoginThread.Start()
                    Do While LoginThread.IsAlive
                        Application.DoEvents()
                        Threading.Thread.Sleep(10)
                    Loop

                    P_LOGINConString = ""

                Catch ex As Exception
                    P_LOGINConString = ""

                    RaiseEvent UserLoginError(P_UserLogin, ex)
                    P_UserLogin = False
                End Try

                Return P_UserLogin
            End Function

            Private Sub LoginWorker()
                Try
                    Dim oWeb As New System.Net.WebClient()
                    oWeb.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
                    Debug.Print(System.Text.Encoding.ASCII.GetString(oWeb.UploadData("https://readitlaterlist.com/v2/auth?", "POST", System.Text.Encoding.ASCII.GetBytes(P_LOGINConString))))
                    P_UserLogin = True
                Catch ex As Exception
                    RaiseEvent UserLoginError(P_UserName, ex)
                    P_UserLogin = False
                End Try
            End Sub

            Public Function Logout() As Boolean
                P_UserLogin = False
                Return Not P_UserLogin
            End Function

            Public Sub WriteXml(ByVal writer As XmlWriter) Implements IXmlSerializable.WriteXml
                writer.WriteElementString("UserName", Me.UserName)
                writer.WriteElementString("UserPassword", Me.UserPassword)
            End Sub

            Public Sub ReadXml(ByVal reader As XmlReader) Implements IXmlSerializable.ReadXml
                Me.UserName = reader.ReadElementString("UserName") : reader.ReadEndElement()
                Me.UserPassword = reader.ReadElementString("UserPassword") : reader.ReadEndElement()

                reader.ReadEndElement()
            End Sub

            Public Function GetSchema() As XmlSchema Implements IXmlSerializable.GetSchema
                Return (Nothing)
            End Function

        End Class

        Public Function Add(ByVal UserCredentials As User) As Boolean
            Try
                P_Credentials.Add(UserCredentials)
                Return True
            Catch ex As Exception
                RaiseEvent Error(ex)
                Return False
            End Try
        End Function

        Public Function Add(ByVal UserName As String, ByVal UserPassword As String) As Boolean
            Try
                P_Credentials.Add(New User(UserName, UserPassword))
                Return True
            Catch ex As Exception
                RaiseEvent Error(ex)
                Return False
            End Try
        End Function

        Public Function Remove(ByVal index As Integer) As Boolean
            Try
                P_Credentials.Remove(P_Credentials.Item(index))
                Return True
            Catch ex As Exception
                RaiseEvent Error(ex)
                Return False
            End Try
        End Function

        Public Function Remove(ByVal UserName As String) As Boolean
            Try
                For i As Integer = 0 To P_Credentials.Count - 1
                    If P_Credentials.Item(i).UserName = UserName Then P_Credentials.Remove(P_Credentials.Item(i))
                Next i

                Return True
            Catch ex As Exception
                RaiseEvent Error(ex)
                Return False
            End Try
        End Function

        Public Function Remove(ByVal UserCredentials As User) As Boolean
            Try
                P_Credentials.Remove(UserCredentials)
                Return True
            Catch ex As Exception
                RaiseEvent Error(ex)
                Return False
            End Try
        End Function

        <XmlIgnore()> _
        Public ReadOnly Property Item(ByVal UserName As String) As User
            Get
                Try
                    For i As Integer = 0 To P_Credentials.Count - 1
                        If P_Credentials.Item(i).UserName = UserName Then Return P_Credentials.Item(i)
                    Next i
                    Return Nothing
                Catch ex As Exception
                    RaiseEvent Error(ex)
                    Return Nothing
                End Try

            End Get
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property Item(ByVal Index As Integer) As User
            Get
                Try
                    Return P_Credentials.Item(Index)
                Catch ex As Exception
                    RaiseEvent Error(ex)
                    Return Nothing
                End Try
            End Get
        End Property

        <XmlArray("UserList")>
        <XmlArrayItem("User")>
        Public Property Item() As List(Of User)
            Get

                Return P_Credentials
            End Get
            Set(ByVal value As List(Of User))
                P_Credentials = value
            End Set
        End Property

        <XmlElement("ApiKey")>
        Public Property PublicKey As String
            Get
                Return P_PublicKey
            End Get
            Set(ByVal value As String)
                P_PublicKey = value
                For Each Item As User In P_Credentials
                    Item.Logout()
                Next

            End Set
        End Property

        Public Sub Clear()
            P_PublicKey = ""
            P_Credentials.Clear()
        End Sub

        Public Function SerializeClass() As Xml.XmlDocument
            Try
                Dim ClassSerializer As New Xml.Serialization.XmlSerializer(GetType(Credentials))
                Dim XmlClass As New StringWriter()
                'Dim TempClass As RIL.Credentials
                ClassSerializer.Serialize(XmlClass, Me)

                Dim XmlDoc As New XmlDocument
                XmlDoc.InnerXml = XmlClass.ToString
                Debug.Print(XmlDoc.InnerXml)
                Return XmlDoc
            Catch ex As Exception
                Return New Xml.XmlDocument
            End Try

        End Function

    End Class



End Class
